/**
 *1、 application.yml文件配置RabbitMQ相关信息；
 *
 * 2、编写配置类，用于创建交换机和队列，并进行绑定
 *
 * 3、注入RabbitTemplate对象，通过RabbitTemplate对象发送消息到交换机
 *   1)send
 *         rabbitTemplate.setExchange("My.Exchange");
 *         rabbitTemplate.setRoutingKey("R.K");
 *         rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
 *         MessageProperties props = new MessageProperties();
 *         props.setExpiration(Long.toString(5000));
 *         Message toSend = new Message("message".toString().getBytes(), props);
 *         rabbitTemplate.send(toSend);
 *   2)sendAndReceive
 *   3)convertAndSend  自动将Java对象包装成Message对象，Java对象需要实现 Serializable 序列化接口，通过 MessagePostProcessor 来添加属性；异步消费
           rabbitTemplate.convertAndSend("direct.exchange","key.1",user,msg -> {
                    mgs.getMessageProperties().setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
                    return msg;
           })
 *   4)convertSendAndReceive  同步消费，只有确定消费者全部接收到消息，才会发送下一条信息
 */
package com.example.rabbitmqspringbootdemo;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class Producer  {
    private static final int count = 5;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RabbitMessagingTemplate rabbitMessagingTemplate;

    public void simpleProducer() {
        String message="简单模式---";
        for (int num = 0; num < count; num++) {
            String msg = message + (num+1);
            //routingKey在使用缺省交换器时就是队列名
            rabbitTemplate.convertAndSend("springSimpleQueue",  msg);
            System.out.println("[SimpleQueue] Sent："+msg);
        }
    }

    //主动收消息
    public void simpleReceive(){
        for (int num = 0; num < count; num++) {
            Message message = rabbitTemplate.receive("springSimpleQueue", 2000);
            if (message!=null)
                 System.out.println(new String(message.getBody()));
            else{
                System.out.println("Simple 队列空，目前没有消息可取");
                break;
            }
        }
        // String msg=(String)rabbitTemplate.receiveAndConvert("springSimpleQueue",2000);
    }

    public void workProducer() {
        String message="工作模式---";
        for (int num = 0; num < count; num++) {
            String msg = message + (num+1);
            rabbitTemplate.convertAndSend("springWorkQueue",  msg);
            System.out.println("[WorkQueue] Sent："+msg);
        }
    }

    public void fanoutProducer() {
        String message="发布订阅模式---";
        for (int num = 0; num < count; num++) {
            String msg = message + (num+1);
            rabbitTemplate.convertAndSend("springFanoutExchange", "", msg);
            System.out.println("[Fanout] Sent："+msg);
        }
    }

    public void directProducer() {
        String routingkey = "one";
        String message="Direct路由模式--"+routingkey + "---";
        for (int num = 0; num < count; num++) {
            String msg = message + (num+1);
            rabbitTemplate.convertAndSend("springDirectExchange", routingkey, msg );
            System.out.println("[Direct] Sent："+msg);
        }
    }

    public void topicProducer() {
        CorrelationData correlationData = new CorrelationData("id" + System.currentTimeMillis() + "");
        String message = "Topic广播模式--恭喜您，注册成功！userid=";
        for (int num = 0; num < count; num++) {
            String msg = message + (num+1);
            rabbitTemplate.convertAndSend(RabbitmqConfig.EXCHANGE_NAME, "topic.sms.email", msg, correlationData);
            System.out.println("[Topic] Sent：" + msg );
        }
    }
}
